/**
 * Hotel List
 */
Divo.app.HotelList = function() {

	/* ----------------------- private属性 ----------------------- */
	var queryUrl = Divo.getPath() + 'servlet/Sample?action=hotelList';
	var gridEl = 'hotel-grid';
	var pageSize = 10;

	var grid, ds, selModel, tb, filterCombo;
	var cId, cIdFocus;
	var bookBtn;

	var oBookingForm;

	/* ----------------------- private方法 ----------------------- */
	// 创建网格
	function createGrid() {
		createToolbarBtn();
		
		var recordDef =  Ext.data.Record.create([
			{name : "id", type : "int"},
			{name : "name", type : "string"},
			{name : "address", type : "string"},
			{name : "city", type : "string"},
			{name : "state", type : "string"},
			{name : "zip", type : "string"},
			{name : "country", type : "string"},
			{name : "price", type : "float"}
		]);

		var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
			header : "Name",
			width : 120,
			sortable : true,
			dataIndex : 'name'
		}, {
			header : "Address",
			width : 120,
			sortable : true,
			dataIndex : 'address'
		}, {
			header : "City",
			width : 120,
			sortable : true,
			dataIndex : 'city'
		}, {
			header : "State",
			width : 100,
			sortable : true,
			dataIndex : 'state'
		}, {
			header : "Zip",
			width : 60,
			sortable : true,
			dataIndex : 'zip'
		}, {
			header : "Country",
			width : 120,
			sortable : true,
			dataIndex : 'country'
		}, {
			header : "Price",
			width : 120,
			sortable : true,
			dataIndex : 'price'
		}
		]);
		
		ds = new Ext.data.Store( {
				proxy : new Ext.data.HttpProxy( {
					url : queryUrl,
					method: 'GET'    //没有这个配置url就无效
				}),
				reader : new Ext.data.JsonReader( {
					root : 'rows',
					totalProperty : 'totalCount',
					id : 'id'
				}, recordDef),
				remoteSort : true
			});

		var config = {
			el : gridEl,
			region : 'center',
			store : ds,
			sm : new Ext.grid.RowSelectionModel( {
				singleSelect : true
			}),
			cm : cm,
			loadMask : Divo.loadMask,
			tbar: tb,
			bbar : new Ext.PagingToolbar( {
				pageSize : pageSize,
				store : ds,
				displayInfo : true
			})
		};
		
   		grid = new Ext.grid.GridPanel(config);
	}

	// 选择某行
	function onRowSelect(grid, rowIndex, e) {
		if (cIdFocus)
			cId = cIdFocus;
		else
			cId = selModel.getSelections()[0].data.id;
		cIdFocus = null;
		setToolbarStatus();
	}

	// 加载数据完成后
	function onLoad(store, records, options) {
		if (ds.getTotalCount() === 0) {
			cId = null;
		} else {
			if (cId)
				selModel.selectRow(ds.indexOfId(cId));
			else
				selModel.selectFirstRow();
		}
		setToolbarStatus();
	}

	// 添加工具条按钮
	function createToolbarBtn() {
		tb = [];
		bookBtn = new Ext.Toolbar.Button( {
			text : 'Booking',
			tooltip : 'Hotel Booking',
			icon : Divo.getIconAdd(),
			cls : 'x-btn-text-icon',
			handler : bookHotel
		});
		tb.push(bookBtn);
		tb.push('->');
		tb.push({text:"Search:&nbsp;&nbsp;"});
		// Create the filter field
		filterCombo = new Ext.ux.form.HistoryClearableComboBox( {
			selectOnFocus : true,
			emptyText : ' ',
			resizable : true,
			hideClearButton : false,
			hideTrigger : false,
			typeAhead : true,
			width:150,
			triggerAction : 'all'
		});
		tb.push(filterCombo);

		// press enter keyboard
		filterCombo.on('specialkey', onSearchKeyPress);
		filterCombo.on('clearvalue', onSearch);
		filterCombo.on('select', onSearch);

		tb.push( {
			icon : Divo.getIconSearch(),
			cls : 'x-btn-icon',
			tooltip : 'Begin to Search(or Press Enter)',
			tooltipType : 'title',
			handler : onSearch
		});
	}
	
	//按回车键开始搜索
	function onSearchKeyPress(o, e) {
		if (e.getKey() === e.ENTER) {
			ds.load();
		}
	}
	
	//搜索
	function onSearch() {
		ds.load();
	}

	// 设置网格工具条按钮状态
	function setToolbarStatus() {
		if (ds.getTotalCount() === 0) {
			bookBtn.disable();
		} else {
			bookBtn.enable();
		}
	}

	// 预订
	function bookHotel() {
		if (!selModel.hasSelection()) {
			Divo.say('Select a hotel');
			return;
		}
		var rec = selModel.getSelections()[0].data;
		oBookingForm.showDialog(rec);
	}

	/* ----------------------- public方法 ----------------------- */
	return {
		/**
		 * 初始化
		 */
		init : function() {
			oBookingForm = Divo.app.BookingForm;

			createGrid();
		},
		/**
		 * 返回Ext网格对象
		 */
		getGrid : function() {
			return grid;
		},
		/**
		 * 展现网格
		 */
		showGrid : function() {
			grid.render();
			selModel = grid.getSelectionModel();

			selModel.on('rowselect', onRowSelect);
			ds.on("load", onLoad);
			ds.on('beforeload', function() {
				ds.baseParams.filter = filterCombo.getValue();
		 	}, this, true);

			ds.load( {
				params : {
					start : 0,
					limit : pageSize
				}
			});
		}

	}; // return

}();

// EOP

